import Server from '@/config/server';
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  mapLists: [],
  homeMapLists: [],
  NewDataLists: [],
  NewDataFields: [],
  StatisticsLists: [],
  StatisticsFields: [],
  SendRegisterLists: [],
  SendRegisterFields: [],
  registerItemList: [],
  registerItemFields: [],
};
const actions = {
  // 列表展示
  getLists({ commit }, page) {
    request.get(Server.action.control_briefing, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setLists', response.data.result.list);
          const { fields } = response.data.result;
          fields.forEach((item, i) => {
            item.dataIndex = item.key;
            item.align = 'center';
            // if(i < fields.length-1) {
            item.width = item.width || 120;
            // }
            if (item.key == 'report') {
              item.width = 320;
              item.scopedSlots = { customRender: item.key };
            }
            if (fields[i].key === 'name') {
              fields[i].scopedSlots = { customRender: fields[i].key };
            }
          });
          // fields[0].fixed = 'left'
          fields[0].width = 90;
          fields[0].scopedSlots = { customRender: fields[0].key };
          fields[1].width = 60;
          commit('setFields', fields);
        } else {

        }
      });
  },
  // 详情
  dataDetail({ commit }, id) {
    request.get(Server.action.equipment_detail + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setDetail', response.data.result);
        // showSuccess(response.data.result)
        } else {
          showError(response.data.message);
        }
      });
  },
  // 设备地图展示
  mapLists({ commit }, page) {
    request.get(Server.action.Map_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          const listData = response.data.result.list;
          console.log(listData);
          listData.data.forEach((item) => {
            item.show = false;
          });
          commit('setMapLists', listData);
        } else {
          showError(response.data.messages);
        }
      });
  },
  // 首页/监控大屏地图
  HomeMapLists({ commit }, page) {
    console.log('地图接口有请求');
    request.get(Server.action.Map_homeList, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          const listData = response.data.result.list;
          console.log(listData);
          // listData.forEach((item) => {
          //   item.show = false;
          // });
          commit('setHomeMapLists', listData);
        } else {
          showError(response.data.messages);
        }
      });
  },
  // 设备实时数据
  getNewDataLists({ commit }, page) {
    request.get(Server.action.data_newData, { params: page })
      .then((response) => {
        console.log('实时数据', response);
        if (response.data.errorCode === 0) {
          const listData = response.data.result.list;
          listData.forEach((item, i) => {
            item.row_num = String(i + 1);
          });
          commit('setNewDataLists', listData);
          const { fields } = response.data.result;
          fields[0].width = 40;
          fields.forEach((item) => {
            item.dataIndex = item.key;
            item.align = 'center';
            item.width = item.width || 100;
            if (item.key == 'item') {
              item.scopedSlots = { customRender: 'trend' };
              item.width = '20%';
            }
          });
          fields.push({
            title: '历史数据',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            width: 100,
          });
          commit('setNewDataFields', fields);
        }
      });
  },
  // 设备统计
  getStatisticsLists({ commit }, page) {
    request.get(Server.action.data_statistics, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          let listData = response.data.result;
          if (listData == null) {
            listData = [];
          } else {
            listData = [listData];
          }
          console.log(listData);
          commit('setStatisticsLists', listData);
          const fields = [
            {
              title: '能耗',
              dataIndex: 'consumption',
              key: 'consumption',
              align: 'center',
              width: 80,
            },
            {
              title: '产量',
              dataIndex: 'yield',
              key: 'yield',
              align: 'center',
              width: 80,
            },
            {
              title: '进车数',
              dataIndex: 'card_num',
              key: 'card_num',
              align: 'center',
              width: 80,
            },
            {
              title: '平均能耗',
              dataIndex: 'unit_consumption',
              key: 'unit_consumption',
              align: 'center',
              width: 80,
            },
            {
              title: '日期(前一天)',
              dataIndex: 'statistics_date',
              key: 'statistics_date',
              align: 'center',
              width: 80,
            },
            {
              title: '历史数据',
              align: 'center',
              scopedSlots: { customRender: 'action' },
              width: 80,
            },
          ];
          commit('setStatisticsFields', fields);
        }
      });
  },
  // 设备控制信息
  getSendRegisterLists({ commit }, page) {
    request.get(Server.action.data_sendRegister, { params: page })
      .then((response) => {
        if (response.data.errorCode === 0) {
          const data = response.data.result;
          data.forEach((item, i) => {
            if (item.type == 2) {
              item.equipment_data == 1 ? item.isSwitch = true : item.isSwitch = false;
            }
            item.row_num = String(i + 1);
          });
          commit('setSendRegisterLists', data);
          const fields = [
            {
              title: '序号',
              dataIndex: 'row_num',
              key: 'row_num',
              align: 'center',
              width: 40,
            },
            {
              title: '数据名称',
              dataIndex: 'name',
              key: 'name',
              align: 'center',
              // scopedSlots: { customRender: 'a' },
              width: 100,
            },
            {
              title: '数据值',
              dataIndex: 'type',
              key: 'type',
              scopedSlots: { customRender: 'type' },
              align: 'center',
              width: 100,
            },
            {
              title: '下发控制',
              dataIndex: 'c',
              key: 'c',
              align: 'center',
              scopedSlots: { customRender: 'c' },
              width: 100,
            },
            {
              title: '历史数据',
              align: 'center',
              scopedSlots: { customRender: 'action' },
              width: 100,
            },
          ];
          commit('setSendRegisterFields', fields);
        }
      });
  },
  // 主动采集
  getNewCollectionFun({ commit }, id) {
    return request.get(Server.action.data_getNewCollection, { params: id, timeout: 300000 });
  },
  // 下发控制
  submitControl({ commit }, fromItem) {
    commit('setErrorCode', false);
    request.get(Server.action.data_sendOutRegister, { params: fromItem })
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
        // commit("setDetail", response.data.result);
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 下发控制历史记录
  getRegisterSendRecordList({ commit }, page) {
    request.get(Server.action.registerSendRecord_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setRegisterItemList', response.data.result.list);
          const { fields } = response.data.result;
          fields.forEach((item, i) => {
            item.dataIndex = item.key;
            item.align = 'center';
            item.width = item.width || 100;
          });
          fields[0].width = 60;
          commit('setRegisterItemFields', fields);
        } else {

        }
      });
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setLists(state, lists) {
	    state.dataLists = lists;
  },
  setFields(state, lists) {
	    state.Fields = lists;
  },
  setDetail(state, data) {
    state.itemDetail = data;
  },
  setMapLists(state, lists) {
    state.mapLists = lists;
  },
  setHomeMapLists(state, lists) {
    state.homeMapLists = lists;
  },
  // 实时数据
  setNewDataLists(state, lists) {
    state.NewDataLists = lists;
  },
  setNewDataFields(state, lists) {
    state.NewDataFields = lists;
  },
  // 统计信息
  setStatisticsLists(state, lists) {
    state.StatisticsLists = lists;
  },
  setStatisticsFields(state, lists) {
    state.StatisticsFields = lists;
  },

  // 控制信息
  setSendRegisterLists(state, lists) {
    state.SendRegisterLists = lists;
  },
  setSendRegisterFields(state, lists) {
    state.SendRegisterFields = lists;
  },
  // 控制信息历史
  setRegisterItemList(state, lists) {
    state.registerItemList = lists;
  },
  setRegisterItemFields(state, lists) {
    state.registerItemFields = lists;
  },
};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
